package com.cyp.blogsystem.service.impl;

import com.cyp.blogsystem.exception.BusinessException;
import com.cyp.blogsystem.mapper.UserMapper;
import com.cyp.blogsystem.mapper.UserProfileMapper;
import com.cyp.blogsystem.model.request.LoginRequest;
import com.cyp.blogsystem.model.request.RegisterRequest;
import com.cyp.blogsystem.model.request.UpdateProfileRequest;
import com.cyp.blogsystem.model.vo.LoginVO;
import com.cyp.blogsystem.pojo.User;
import com.cyp.blogsystem.pojo.UserProfile;
import com.cyp.blogsystem.service.UserService;
import com.cyp.blogsystem.utils.JwtUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import lombok.RequiredArgsConstructor;
import cn.hutool.core.util.StrUtil;

@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

  private final UserMapper userMapper;
  private final BCryptPasswordEncoder passwordEncoder;
  private final JwtUtils jwtUtils;
  private final UserProfileMapper userProfileMapper;

  @Override
  @Transactional
  public User register(RegisterRequest request) {
    // 1. 数据校验
    // 1.1 检查用户名是否已存在
    if (userMapper.selectByUsername(request.getUsername()) != null) {
      throw new BusinessException("用户名已存在");
    }
    // 1.2 检查邮箱是否已存在
    if (userMapper.selectByEmail(request.getEmail()) != null) {
      throw new BusinessException("邮箱已存在");
    }

    // 2. 创建用户
    // 2.1 构建用户对象
    User user = new User();
    user.setUsername(request.getUsername());
    // 2.2 密码加密
    user.setPassword(passwordEncoder.encode(request.getPassword()));
    user.setEmail(request.getEmail());
    user.setStatus(1);

    // 3. 保存用户信息
    userMapper.insert(user);

    // 4. 返回用户信息
    return user;
  }

  @Override
  public LoginVO login(LoginRequest request) {
    // 查询用户
    User user = userMapper.selectByUsername(request.getUsername());
    if (user == null || !passwordEncoder.matches(request.getPassword(), user.getPassword())) {
      throw new BusinessException("用户名或密码错误");
    }

    // 检查用户状态
    if (user.getStatus() != 1) {
      throw new BusinessException("账号已被禁用");
    }

    // 生成token
    String token = jwtUtils.generateToken(user.getId());

    // 返回登录信息
    LoginVO loginVO = new LoginVO();
    loginVO.setUserId(user.getId());
    loginVO.setUsername(user.getUsername());
    loginVO.setToken(token);
    return loginVO;
  }

  @Override
  public User getUserById(Long id) {
    return userMapper.selectById(id);
  }

  @Override
  @Transactional
  public void updateProfile(Long userId, UpdateProfileRequest request) {
    UserProfile userProfile = userProfileMapper.selectByUserId(userId);
    if (userProfile == null) {
      throw new BusinessException("用户不存在");
    }

    // 更新用户信息
    if (StrUtil.isNotBlank(request.getNickname())) {
      userProfile.setNickname(request.getNickname());
    }
    if (StrUtil.isNotBlank(request.getAvatar())) {
      userProfile.setAvatar(request.getAvatar());
    }

    userProfileMapper.updateByUserId(userProfile);
  }
}